﻿using System;
using System.Xml.Linq;

namespace MsSqlMetaDataProvider
{
    /// <summary>
    /// Column metadata class
    /// </summary>
    public partial class COLUMN
    {
        /// <summary>
        /// Gets a value indicating whether this column is nullable.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this column is nullable; otherwise, <c>false</c>.
        /// </value>
        public bool IsNullable
        {
            get { return IS_NULLABLE == "YES"; }
        }

        /// <summary>
        /// Gets a value indicating whether this column has default value specified in Database.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this column has default; otherwise, <c>false</c>.
        /// </value>
        public bool HasDefault
        {
            get { return !string.IsNullOrEmpty(COLUMN_DEFAULT); }
        }

        /// <summary>
        /// Gets the .net type of the column.
        /// </summary>
        /// <value>
        /// The type of the net.
        /// </value>
        public Type NetType
        {
            get
            {
                switch (DATA_TYPE)
                {
                    case ("bigint"):
                        return typeof (long);
                    case ("sql_variant"):
                        return typeof (object);
                    case ("bit"):
                        return typeof (bool);
                    case ("char"):
                    case ("nchar"):
                    case ("ntext"):
                    case ("nvarchar"):
                    case ("nvarchar(max)"):
                    case ("text"):
                    case ("varchar"):
                    case ("varchar(max)"):
                        return typeof (string);
                    case ("datetime"):
                    case ("datetime2"):
                    case ("smalldatetime"):
                    case ("date"):
                        return typeof (DateTime);
                    case ("decimal"):
                    case ("money"):
                    case ("numeric"):
                    case ("smallmoney"):
                        return typeof (decimal);
                    case ("float"):
                        return typeof (double);
                    case ("image"):
                    case ("timestamp"):
                    case ("varbinary"):
                    case ("binary"):
                        return typeof (byte[]);
                    case ("int"):
                        return typeof (int);
                    case ("real"):
                        return typeof (float);
                    case ("smallint"):
                        return typeof (short);
                    case ("tinyint"):
                        return typeof (byte);
                    case ("uniqueidentifier"):
                        return typeof (Guid);
                    case ("xml"):
                        return typeof (XElement);
                    default:
                        return typeof (object);
                }
            }
        }

        /// <summary>
        /// Gets the name of .net type of the column.
        /// </summary>
        /// <remarks>Used in templates to get the string value.</remarks>
        /// <value>
        /// The name of the .net type.
        /// </value>
        public string NetTypeName
        {
            get { return NetType.ToString(); }
        }
    }
}